<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="项目" prop="grantId">
        <el-select v-model="queryParams.grantId" placeholder="请选择项目">
          <el-option
            v-for="dict in grantOptions"
            :key="dict.grantId"
            :label="dict.grantName"
            :value="dict.grantId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportListSelfPlan"
          v-hasPermi="['grant:companyplan:export']"
        >导出</el-button>
      </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="showConfigCel=true"
          >列设置</el-button>
        </el-col>
    </el-row>
    <div class="itemTrading">
    <el-table v-loading="loading" :data="companyplanList" @selection-change="handleSelectionChange"  :cell-style="cellStyle" show-summary stripe>
      <template v-for='item in fieldList'>
        <el-table-column :label="item.value" :prop="item.key" :key="item.key" v-if="showcelstrs==''||showcelstrs.indexOf(';'+item.key+';')>=0">
        </el-table-column>
      </template>
    </el-table>
    </div>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-drawer
      title="设置列"
      :visible.sync="showConfigCel"
      :direction="direction"
      :size="'900px'"
    >
      <div class="querywhere">
        <el-checkbox-group v-model="configshowcel" size="small">
          <el-checkbox
            border
            v-for="(item, index) in fieldList"
            :key="item.key"
            :label="item.key"
            >{{ item.value }}</el-checkbox
          >
        </el-checkbox-group>
        <el-button type="info" @click="configshowcel = []">清空</el-button>
        <el-button type="primary" @click="showConfigCel = false;handleQuery();">确认</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {  getCompanyplan, delCompanyplan, addCompanyplan, updateCompanyplan, exportCompanyplan,createComapnyPlan,listSelfPlan,exportPlanForSubject,getCompanyPlanForSubject,exportForSubject,getCompanyPlanForSubjectSelf } from "@/api/grant/companyplan";
import {listGrantAll} from "@/api/grant/grant";
import {getToken} from "@/utils/auth";

export default {
  name: "Companyplan",
  data() {
    return {
      // 遮罩层
      loading: true,
      showConfigCel:false,
      configshowcel: [],
      showcelstrs:'',
      direction: 'rtl',
      // 选中数组
      ids: [],
      grantOptions: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      totalself: 0,
      // 派出单位计划管理表格数据
      companyplanList: [],
      showCompanyplanList: [],
      fieldList:[],
      selfPlanList:[],
      selfFieldList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      selfOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        grantId: undefined,
        companyId: undefined,
        subjectCode: undefined,
        planNum: undefined,
        isExportTemplate:'N',
        configshowcelSearch: undefined
      },
      selfParams:{
        pageNum: 1,
        pageSize: 10,
        grantId: undefined,
        companyId: undefined,
        subjectCode: undefined,
        planNum: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    let query ={};
    query["isEnabled"]="0";
    listGrantAll(query).then(res=>{
      this.grantOptions = res.data;
      this.queryParams.grantId=res.data[res.data.length-1].grantId;
      this.getList();
    })
  },
  methods: {
    /** 查询派出单位计划管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.isExportTemplate = 'Y';
      getCompanyPlanForSubjectSelf(this.queryParams).then(response => {
        this.companyplanList = response.data["ROWMAPLIST"];
        this.fieldList = response.data["FIELDMAP"];
        if(this.configshowcel.length == 0)
        {
          for(let ni=0;ni<this.fieldList.length;ni++)
           {
              this.configshowcel.push(this.fieldList[ni].key);
           }
          this.showCompanyplanList = response.data["FIELDMAP"];
          this.showcelstrs = ';'+this.configshowcel.join(";")+';';
        }else
        {
           this.showcelstrs = ';'+this.configshowcel.join(";")+';';
           for(let ni=0;ni<this.fieldList.length;ni++)
           {
              if(this.showcelstrs.indexOf(this.fieldList[ni].key+';')>=0){
                this.showCompanyplanList.push(this.fieldList[ni]);
              }
           }
        }
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        grantId: undefined,
        companyId: undefined,
        subjectCode: undefined,
        planNum: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出本单位计划操作 */
    exportListSelfPlan() {
      const queryParams = this.queryParams;
      queryParams.isExportTemplate='Y';
      queryParams.configshowcelSearch = this.configshowcel.join(",");
      this.$confirm('是否确认导出本单位派出计划?', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPlanForSubject(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    cellStyle(row,column,rowIndex,columnIndex){
      if(row.columnIndex%2==1){
        return "background:rgb(201, 218, 248)";
      }
    }
  }
};
</script>
<style>
.itemTrading .el-table{
  overflow: auto;
}
.itemTrading  .el-table__body-wrapper,
.itemTrading  .el-table__header-wrapper,
.itemTrading  .el-table__footer-wrapper {
  overflow: visible;
}
.itemTrading  .el-table::after {
  position: relative !important;
}
  .body-theme-grenn .el-table--scrollable-x .el-table__body-wrapper{
    overflow: visible;
  }
  .querywhere {
  margin: 5px;
}
.el-drawer.rtl{
    overflow: scroll;
}
.querywhere >>> .el-checkbox.is-bordered.el-checkbox--small {
  padding: 8px;
  width: 122px;
}
.querywhere >>> .el-checkbox.is-bordered + .el-checkbox.is-bordered {
  margin-left: 0px;
}
.querywhere >>> .el-checkbox {
  margin-right: 10px;
}
</style>
